package com.example.myspringboot.controller;

import com.example.myspringboot.business.PdfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

@Controller
@RequestMapping("/pdf")
public class PdfController {

    @Autowired
    private PdfService pdfService;

    @RequestMapping("/view")
    public ModelAndView view() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pdf");
        modelAndView.addObject("url", "D://");
        return modelAndView;
    }

    @RequestMapping("/show")
    public void showPdf(HttpServletRequest request, HttpServletResponse response) throws IOException {
        byte[] buffer = pdfService.getPdf();
        OutputStream outputStream = response.getOutputStream();
        outputStream.write(buffer);
        response.setContentType("application/pdf");
        outputStream.close();
    }
}
